<?php

class AdvertisersAction extends AdminAction
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 广告主信息
     */
    public function index()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $advertiser_model = M('advertisers');
        $advertiser_info = $advertiser_model->where(array('user_id'=>$user_id))->find();

        //如果没有该广告主信息就跳转填写信息页面
        if (empty($advertiser_info)) {
            header("Location: " . $this->baseUrl . "/Admin/Advertisers/add");
        }

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('advertiser_info', $advertiser_info);
        $this->display();
    }

    /**
     * 试图：添加广告主
     */
    public function add()
    {
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();
    }

    /**
     * 动作：添加广告主
     */
    public function store()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $data = array();

        $data['user_id']                 = $user_id;
        $data['username']                = I('post.username');
        $data['phone']                   = I('post.phone');
        $data['email']                   = I('post.email');
        $data['qq']                      = I('post.qq');
        $data['financial_information']   = I('post.financial_information');//财务信息
        $data['registrant_information']  = I('post.registrant_information');//注册人信息
        $data['status']                  = 1;
        $data['create_time']             = time();

        if (M('Advertisers')->add($data)) {
            alert('success',' 提交成功',$this->baseUrl.'/Admin/Advertisers/index');
        }
        alert('error','提交失败');
    }

    /**
     * 视图：编辑
     */
    public function edit()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $id = I('get.id');

        $map['user_id']       = $user_id;
        $map['advertiser_id'] = $id;
        $advertiser_info = M('Advertisers')->where($map)->find();

        $this->assign('advertiser_info',$advertiser_info);
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();
    }

    /**
     * 动作：编辑
     */
    public function update()
    {
        $data = array();

        $data['advertiser_id']           = I('post.advertiser_id');
        $data['username']                = I('post.username');
        $data['phone']                   = I('post.phone');
        $data['email']                   = I('post.email');
        $data['qq']                      = I('post.qq');
        $data['financial_information']   = I('post.financial_information');//财务信息
        $data['registrant_information']  = I('post.registrant_information');//注册人信息
        $data['status']                  = 1;
        $data['update_time']             = time();

        if (M('Advertisers')->save($data)) {
            alert('success',' 提交成功',$this->baseUrl.'/Admin/Advertisers/index');
        }
        alert('error','提交失败');

    }



}


